/**
 * Created by De Niro on 15-4-22.
 */
app.factory('hyMessage',function(hyUser){
    //['inviteMessage',群聊id,hyUser.id,加入member的nickname集合]
    //['message',hyUser.id,接收者id,聊天内容]
    var temp = localStorage.getItem('hyMessage'+hyUser.id);//读入聊天记录
    var hyMessage;
    if( temp != null ){
        hyMessage = $.parseJSON(temp);
    }else{
        hyMessage = [];
    }
    hyMessage.save = function(){
        var tempString = JSON.stringify(hyMessage);
        localStorage.setItem('hyMessage'+hyUser.id,tempString);
    }
    return hyMessage;
}).factory('hyContact',function($http,$q,hyUser){
    var temp = localStorage.getItem('hyContactHistory'+hyUser.id);//读入聊过天的联系人
    var hyContact = {};
    if( temp != null ){
        hyContact.data = $.parseJSON(temp);
    }else{
        hyContact.data = {};
    }
    hyContact.save = function(){
        var temp;
        for(temp in hyContact.data){
            if(hyContact.data.hasOwnProperty(temp)){
                if(!hyContact.data[temp]['group']){
                    hyContact.data[temp]['online'] = false;
                    if(!hyContact.data[temp]['everTalk'] ){
                        delete hyContact.data[temp];
                    }
                }
            }
        }
        var tempString = JSON.stringify(hyContact.data);
        localStorage.setItem('hyContactHistory'+hyUser.id,tempString);
    }
    hyContact.syncPromise = function(url){
        console.log(hyContact.data);
        //从服务器同步当前在线的联系人,添加到从localStorage读出的联系人中
        var promise = $http.get(url).then(function(response) {
            if (typeof response.data === 'object') {
                delete response.data.data[hyUser.id];
                var temp,tempContact;
                for(temp in response.data.data){
                    if(response.data.data.hasOwnProperty(temp)){
                        if(hyContact.data[temp]){
                            tempContact = response.data.data[temp];
                            tempContact['latestTalk'] = hyContact.data[temp]['latestTalk'] ;
                            tempContact['latestTime'] = hyContact.data[temp]['latestTime'] ;
                            tempContact['unChecked'] = hyContact.data[temp]['unChecked'];
                            hyContact.data[temp] = tempContact;
                            hyContact.data[temp]['everTalk'] = true;
                        }else{
                            hyContact.data[temp] = response.data.data[temp];
                        }
                    }
                }
                return hyContact;
            } else {
                return $q.reject(response.data);
            }
        }, function(response) {
            return $q.reject(response.data);
        });
        return promise;
    };
    return hyContact;
}).factory('hyPushStream',function($location){
    //hyPushStream 需配置onstatuschange/onmessage 然后在connectToChanner(channel)
    var domain = $location.host().split('.')[1];
    PushStream.LOG_LEVEL = 'debug';
    var ps = new PushStream({
        host: "m."+domain+".cn",//这个地方配盒端push服务器的域名，格式为m.{%$domain%}.cn
        port: "80",//push服务器的端口，默认80
        timeout: "20000",
        modes: "websocket|longpolling|stream|eventsource"//4种模式，哪种写在前面，就优先用哪种
        //通常用websocket与longpolling这两种，前者有的手机浏览器支持不好，后者大部分浏览器都可以
        //websocket稳定性较好，longpolling相对较弱，容易丢消息
    });
    ps.connectToChannel = function(channel){
        ps.removeAllChannels();
        try
        {
            ps.addChannel(channel);
            ps.connect();
        }
        catch (error)
        {
            alert(error);
        }
    };
    return ps;
});
